package net.sxnic.workflow.whimpl;

import net.sxnic.workflow.context.WfContextImpl;
import net.sxnic.workflow.handler.AbstractHandler;
import net.sxnic.workflow.handler.HandlerExt;
import net.sxnic.workflow.handler.WfHandlerException;
import net.sxnic.workflow.task.Task;
import net.sxnic.workflow.wfentity.WfEntity;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FindEntityHandler extends AbstractHandler<WfEntity> implements
		HandlerExt<WfEntity> {
	
	private static Logger logger = LoggerFactory.getLogger(FindEntityHandler.class);

	public FindEntityHandler(WfContextImpl<WfEntity> context) {
		super(context);
	}

	@Override
	public void editExecute() throws WfHandlerException {		
		if(StringUtils.isNotBlank(context.getTaskId())){
			Task task = context.getTaskManager().get(context.getTaskId());
			context.setTask(task);
			//context.setEntity(task.getEntity());
			context.setEntityId(context.getEntity().getId());
			logger.debug("===invoke==edit===success====entityId==="+context.getEntity());
		}
		
		if(StringUtils.isNotBlank(context.getEntityId())){
			context.setEntity(context.getWfenManager().get(context.getEntityId()));
			logger.debug("===invoke==edit===success====entityId==="+context.getEntity());
		}
		
		logger.debug("===查询所有意见==="+context.getCommentMap().size());
	}

	@Override
	public void saveExecute() throws WfHandlerException {
		editExecute();
	}

	@Override
	public void viewExecute() throws WfHandlerException {
		editExecute();
	}

	@Override
	public void submitExecute() throws WfHandlerException {		
		editExecute();
	}
}
